package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.module;

import com.google.common.collect.Lists;
import com.iwhalecloud.bss.kite.client.dto.inst.InstAttrDTO;
import com.iwhalecloud.bss.kite.client.vo.scene.ModuleVO;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.NotApprovedInfoVO;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.SceneNewOfferSeneVO;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.service.util.CopyToDtoUtil;
import com.iwhalecloud.bss.kite.web.service.accept.bo.AcceptContext;
import com.iwhalecloud.bss.kite.web.service.accept.bo.SceneModule;
import com.iwhalecloud.bss.kite.web.service.accept.handler.module.AbstractModuleHandler;
import com.ztesoft.bss.common.util.SpringUtil;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class NotApprovedInfoModuleHandler extends AbstractModuleHandler {

    @Override
    public List<? extends ModuleVO> execute(AcceptContext context, SceneModule sceneModule) {
        NotApprovedInfoVO notApprovedInfoVO = new NotApprovedInfoVO();
        IQuerySceneInstService querySceneInstService = SpringUtil.getBean(IQuerySceneInstService.class);
        String sceneInstId = context.getAcceptRequest().getSceneInstId();
        String nodeCode = querySceneInstService.getExtParameterValue(sceneInstId, "nodeCode", "OrderInput");
        // 重提才展示
        if (!KiteStringUtils.equalsAny(nodeCode, "OrderResubmit")) {
            return Lists.newArrayList();
        }
        List<KiteAttr> commonAttrList = querySceneInstService.listCommonAttrsByModuleId(sceneInstId, "144");
        if (KiteListUtils.isNotEmpty(commonAttrList)) {
            commonAttrList.forEach(commonAttr -> {
                if (KiteStringUtils.isEmpty(commonAttr.getValue())) {
                    commonAttr.setValue("--");
                    commonAttr.setValueDesc("--");
                }
            });
            List<InstAttrDTO> instAttrDTOList = CopyToDtoUtil.transInstAttrDtoList(commonAttrList);
            // 信息返回空，不展示
            if (KiteListUtils.isEmpty(instAttrDTOList)) {
                return Lists.newArrayList();
            }
            notApprovedInfoVO.setNotApprovedAttrs(instAttrDTOList);
            SceneNewOfferSeneVO sceneAccountInfoVo = (SceneNewOfferSeneVO) context.getSceneVo();
            sceneAccountInfoVo.setNotApprovedInfoVO(notApprovedInfoVO);
        }
        return Collections.singletonList(notApprovedInfoVO);
    }

    @Override
    public List<ModuleVO> previewSceneVo(AcceptContext context, SceneModule sceneModule) {
        NotApprovedInfoVO notApprovedInfoVO = new NotApprovedInfoVO();
        IQuerySceneInstService querySceneInstService = SpringUtil.getBean(IQuerySceneInstService.class);
        String sceneInstId = context.getAcceptRequest().getSceneInstId();
        String nodeCode = querySceneInstService.getExtParameterValue(sceneInstId, "nodeCode", "OrderInput");
        // 重提才展示
        if (!KiteStringUtils.equalsAny(nodeCode, "OrderResubmit")) {
            return Lists.newArrayList();
        }
        List<KiteAttr> commonAttrList = querySceneInstService.listCommonAttrsByModuleId(sceneInstId, "144");
        if (KiteListUtils.isNotEmpty(commonAttrList)) {
            //设置预览模式
            commonAttrList.forEach((attr) -> {
                attr.setAttrValueTypeId("TT");
                if (KiteStringUtils.isEmpty(attr.getValue())) {
                    attr.setValue("--");
                    attr.setValueDesc("--");
                }
            });
            List<InstAttrDTO> instAttrDTOList = CopyToDtoUtil.transInstAttrDtoList(commonAttrList);
            // 信息返回空，不展示
            if (KiteListUtils.isEmpty(instAttrDTOList)) {
                return Lists.newArrayList();
            }
            notApprovedInfoVO.setNotApprovedAttrs(instAttrDTOList);
            SceneNewOfferSeneVO sceneAccountInfoVo = (SceneNewOfferSeneVO) context.getSceneVo();
            sceneAccountInfoVo.setNotApprovedInfoVO(notApprovedInfoVO);
        }
        return Collections.singletonList(notApprovedInfoVO);
    }
}
